package cn.com.qiancheng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.com.qiancheng.dto.Student;
import cn.com.qiancheng.util.DbUtil;

public class AddStuDao {
	public int addStudent(String userName, String realName, String password, int classId) {
		int i = 0;
		try {
			Connection con = DbUtil.openConnection();
			String sql = "INSERT INTO "
					+ "t_students("
					+ "stu_user_name,"
					+ "stu_password,"
					+ "stu_of_class,"
					+ "stu_real_name) "
					+ "VALUES(?,?,?,?)";
			PreparedStatement stat = con.prepareStatement(sql);
			stat.setString(1, userName);
			stat.setString(2, password);
			stat.setInt(3, classId);
			stat.setString(4, realName);
			i = stat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	public int UpdateStudent(Student student) {
		int i = 0;
		try {
			Connection con = DbUtil.openConnection();
			String sql = "Update t_students SET " 
						+ "stu_password = ?," 
						+ "stu_of_class = ?," 
						+ "stu_real_name = ?,"
						+ "l_num = ? " 
						+ "WHERE " 
						+ "stu_id = ?";
			PreparedStatement stat = con.prepareStatement(sql);
			stat.setString(1, student.getStuPassword());
			stat.setInt(2, student.getStuClass());
			stat.setString(3, student.getStuRealName());
			stat.setInt(4, student.getStatus());
			stat.setInt(5, student.getStuId());
			i = stat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}
}
